<?php
define('IN_SEXMALL', true);

require(dirname(__FILE__) . '/includes/init.php');
$exc   = new exchange(TABLEPRE."carriage", $db, 'record_id', 'act_name');

if ($_REQUEST['act'] == 'list')
{
	admin_priv('activity_carriage');

	$carriages = get_carriage_list();

	$smarty->assign('carriages',   	$carriages['arr']);
	$smarty->assign('filter',       $carriages['filter']);
	$smarty->assign('record_count', $carriages['record_count']);
	$smarty->assign('page_count',   $carriages['page_count']);

	$smarty->assign('ur_here',     '运费活动');
	$smarty->assign('action_link', array('text' => '添加运费活动', 'href' => 'carriage.php?act=add'));
	$smarty->assign('full_page',   1);

	$smarty->display('carriage_list.htm');
}

elseif ($_REQUEST['act'] == 'query')
{
	check_authz_json('activity_carriage');

	$carriage = get_carriage_list();

	$smarty->assign('carriages',    $carriage['arr']);
	$smarty->assign('filter',          $carriage['filter']);
	$smarty->assign('record_count',    $carriage['record_count']);
	$smarty->assign('page_count',      $carriage['page_count']);

	$sort_flag  = sort_flag($carriage['filter']);
	$smarty->assign($sort_flag['tag'], $sort_flag['img']);

	make_json_result($smarty->fetch('carriage_list.htm'), '',
	array('filter' => $carriage['filter'], 'page_count' => $carriage['page_count']));
}

else if ($_REQUEST['act'] == 'add')
{
	admin_priv('activity_carriage');

	$smarty->assign('ur_here',       '添加运费活动');
	$smarty->assign('action_link',   array('href' => 'carriage.php?act=list', 'text' => '运费活动列表'));

	$smarty->assign('form_act', 'insert');
	$smarty->assign('action',   'add');

	//assign_query_info();
	$smarty->display('carriage_info.htm');
}

elseif ($_REQUEST['act'] == 'insert')
{
	admin_priv('activity_carriage');

	/* 初始化变量 */
	$act_name 		= !empty($_POST['act_name']) ? trim($_POST['act_name']) : '';
	$free_carriage 	= !empty($_POST['free_carriage']) ? trim($_POST['free_carriage']) : 0;
	$free_count 	= !empty($_POST['free_count']) ? intval($_POST['free_count']) : 0;
	$type			= !empty($_POST['type']) ? intval($_POST['type']) : 0;

	/* 获得开始时期与结束日期 */
	$start_time = strtotime($_POST['start_time']);
	$stop_time   = strtotime($_POST['stop_time']);

	$data = array(
    	'act_name'		=> $act_name,
    	'type'			=> $type,
    	'free_count'	=> $free_count,
		'free_carriage'	=> $free_carriage,
    	'start_time'	=> $start_time,
    	'stop_time'		=> $stop_time,
    	'add_time'		=> local_time(),
	);

	$db->autoExecute(TABLEPRE."carriage", $data, 'INSERT');

	/* 记录管理员操作 */
	admin_log($act_name, 'add', '运费活动');

	clear_cache_files(); // 清除模版缓存

	$link[1]['text'] = '运费活动列表';
	$link[1]['href'] = 'carriage.php?act=list';

	$link[2]['text'] = '继续添加';
	$link[2]['href'] = 'carriage.php?act=add';

	sys_msg('添加' . "&nbsp;" .$act_name . "&nbsp;" . '成功',0, $link);

}

elseif ($_REQUEST['act'] == 'edit')
{
	admin_priv('activity_carriage');

	$record_id = !empty($_GET['record_id']) ? intval($_GET['record_id']) : 0;

	$sql = "SELECT record_id, act_name, type, free_count, free_carriage, start_time, stop_time, add_time".
		" FROM ".TABLEPRE."carriage WHERE record_id = '".$record_id."'";
	$carriage = $db->getRow($sql);

	$carriage['start_time'] = date("Y-m-d H:i:s", $carriage['start_time']);
	$carriage['stop_time'] = date("Y-m-d H:i:s", $carriage['stop_time']);

	$smarty->assign('ur_here',       '运费活动编辑');
	$smarty->assign('action_link',   array('href' => 'carriage.php?act=list', 'text' => '运费活动列表'));
	$smarty->assign('form_act',      'update');
	$smarty->assign('action',        'edit');
	$smarty->assign('carriage',      $carriage);

	assign_query_info();
	$smarty->display('carriage_info.htm');
}

elseif ($_REQUEST['act'] == 'update')
{
	admin_priv('activity_carriage');

	/* 初始化变量 */
	$record_id 		= !empty($_POST['record_id']) ? intval($_POST['record_id']) : 0;
	$act_name 		= !empty($_POST['act_name']) ? trim($_POST['act_name']) : '';
	$free_carriage 	= !empty($_POST['free_carriage']) ? trim($_POST['free_carriage']) : 0;
	$free_count 	= !empty($_POST['free_count']) ? intval($_POST['free_count']) : 0;
	$type			= !empty($_POST['type']) ? intval($_POST['type']) : 0;

	/* 获得开始时期与结束日期 */
	$start_time 	= strtotime($_POST['start_time']);
	$stop_time   	= strtotime($_POST['stop_time']);

	$data = array(
    	'act_name'		=> $act_name,
    	'type'			=> $type,
    	'free_count'	=> $free_count,
		'free_carriage'	=> $free_carriage,
    	'start_time'	=> $start_time,
    	'stop_time'		=> $stop_time,
    	'add_time'		=> local_time(),
	);

	$db->autoExecute(TABLEPRE."carriage", $data, 'UPDATE', 'record_id='.$record_id);

	/* 记录管理员操作 */
	admin_log($act_name, 'edit', '运费活动');

	clear_cache_files(); // 清除模版缓存

	$link[0]['text'] = '运费活动列表';
	$link[0]['href'] = 'carriage.php?act=list';

	sys_msg('编辑' . "&nbsp;" .$act_name . "&nbsp;" . '成功',0, $link);

}

elseif ($_REQUEST['act'] == 'remove')
{
	admin_priv('activity_carriage');

	$record_id = !empty($_GET['id']) ? intval($_GET['id']) : 0;
	$exc->drop($record_id);

	admin_log('', 'remove', '运费活动');

	$url = 'carriage.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

	wap_header("Location: $url\n");
	exit;
}

/* 获得文章列表 */
function get_carriage_list()
{
	$result = get_filter();
	if ($result === false)
	{
		$filter = array();
		$filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'record_id' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

		/* 总数 */
		$sql = "SELECT COUNT(*) FROM ".TABLEPRE."carriage";
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);

		$filter = page_and_size($filter);

		/* 获取数据 */
		$sql = "SELECT record_id, act_name, type, free_count, free_carriage, start_time, stop_time, add_time FROM ".TABLEPRE."carriage ";
		set_filter($filter, $sql);

	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}
	$arr = array();
	$res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

	while ($rows = $GLOBALS['db']->fetchRow($res))
	{
		switch ($rows['type'])
		{
			case 1 : $rows['type'] = '金额';break;
			case 2 : $rows['type'] = '数量';break;
		}

		$rows['start_time'] = date('Y-m-d H:i:s', $rows['start_time']);
		$rows['stop_time'] = date('Y-m-d H:i:s', $rows['stop_time']);

		$arr[] = $rows;
	}
	return array('arr' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}